package com.china08.yxyapi.api.manage.sch.platform.xiaoben.kecheng;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;

@CrossOrigin
@RestController
public class KeChengCatalogController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicCatalogRepository catalogRepository;

    @Autowired
    YxyLessonsSchoolCatalogRepository schoolRepository;

    @RequestMapping(value = "/api_yw/manage/sch/xiaoben/kecheng/catalog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        String catalogId1 = reqModel.catalogId1;
        Integer level = reqModel.level;

        List<YxyLessonsSchoolCatalog> catalogs = new ArrayList<YxyLessonsSchoolCatalog>();

        // 获取所有一级栏目
        if (StringUtils.isBlank(catalogId1)) {
            catalogs = schoolRepository.findBySchoolIdAndLevelOrderByOrderNum(schoolId, level);
        }
        // 获取二级栏目
        if (StringUtils.isNotBlank(catalogId1)) {
            catalogs = schoolRepository.findBySchoolIdAndPidAndLevelOrderByOrderNum(schoolId, catalogId1, level);
        }

        if (catalogs.size() > 0) {
            YxyLessonsSchoolCatalog yxyLessonsTopicCatalog = new YxyLessonsSchoolCatalog();
            yxyLessonsTopicCatalog.setId("");
            if (level == 1) {
                yxyLessonsTopicCatalog.setName("全部一级栏目");
            }
            if (level == 2) {
                yxyLessonsTopicCatalog.setName("全部二级栏目");
            }
            catalogs.add(0, yxyLessonsTopicCatalog);
        }

        List<RespModel> returnList = new ArrayList<>();
        for (YxyLessonsSchoolCatalog catalog : catalogs) {
            String id = catalog.getId();
            String name = catalog.getName();
            RespModel respModel = new RespModel();
            respModel.id = id;
            respModel.name = name;
            returnList.add(respModel);
        }
        return returnList;
    }

    public static class ReqModel extends YxyApiPageReqModel {
        public Integer level;
        public String catalogId1;
    }

    public static class RespModel extends YxyApiRespModel {
        public String id;
        public String name;
    }
}
